package com.sfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sfx.domain.ResponseResult;
import com.sfx.domain.entity.User;
import com.sfx.domain.vo.CommentVo;
import com.sfx.domain.vo.PageVo;
import com.sfx.service.UserService;
import com.sfx.utils.BeanCopyUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfx.domain.entity.Comment;
import com.sfx.mapper.CommentMapper;
import com.sfx.service.CommentService;

import static com.sfx.constants.SystemConstants.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-12
 * Time: 17:53
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    /**
     * 只查询对应文章的根评论,不包含子评论
     * 查询评论列表
     * commentType : 1 代表普通评论
     * commentType : 0 代表友链评论
     * @return
     */
    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //1.只查询对应文章的评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //2.普通评论才去根据文章id去查询
        queryWrapper.eq(commentType.equals(COMMENT_TYPE_NORMAL),Comment::getArticleId, articleId);
        //3.只查询对应文章的根评论,rootId==-1
        queryWrapper.eq(Comment::getRootId, COMMENT_IS_ROOT);
        //4.按照类型去查询(友链评论 or 普通评论)
        queryWrapper.eq(Comment::getType,commentType);
        //5.分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        //6.封装为CommentVO
        List<CommentVo> commentVos = getCommentVOS(page.getRecords());
        //7.查询评论对应的子评论
        getChild(commentVos);
        //8.封装为pageVO,返回
        PageVo pageVO = new PageVo(commentVos, page.getTotal());
        return ResponseResult.okResult(pageVO);
    }

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        save(comment);
        return ResponseResult.okResult();
    }

    private void getChild(List<CommentVo> commentVos) {
        commentVos.forEach(commentVo -> {
            //5.1如果该评论的root_id=commentVO的,那么就是它的子评论
            LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Comment::getRootId, commentVo.getId());
            //5.2按照评论发送时间升序排序
            wrapper.orderByAsc(Comment::getCreateTime);
            List<CommentVo> childVOList = getCommentVOS(list(wrapper));
            commentVo.setChildren(childVOList);
        });
    }

    @NotNull
    private List<CommentVo> getCommentVOS(List<Comment> comments) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(comments, CommentVo.class);
        //5.将toCommentUserName,username进行赋值
        commentVos.forEach(commentVo -> {
                    //5.1根据createBy来查询username
                    User user = userService.getById(commentVo.getCreateBy());
                    commentVo.setUsername(user.getNickName());
                    //5.2根据toCommentUserId来查询toCommentUserName
                    //排除有可能直接对文章的回复评论,它就是一个根评论
                    if (commentVo.getToCommentUserId() != -1) {
                        user = userService.getById(commentVo.getToCommentUserId());
                        commentVo.setToCommentUserName(user.getNickName());
                    }
                }
        );
        return commentVos;
    }
}